package com.swipemaster.moves;

import com.swipemaster.common.Const;
import com.swipemaster.common.Coords;
import com.swipemaster.elements.Element;

/** Move around the circle. */
public class CircleMove extends Move
{
	/** Center point of movement circle. */
	protected final int centerX, centerY;
	/** Radius of movement circle, in universal units. */
	protected final double radius;
	/** Radius speed. */
	protected double speed;
	/** Direction of spinning, either left or right. */
	protected final int direction;
	/** Current degree. */
	protected int degree;

	/** @param centerX Center point of movement circle, x. Universal units.
	 * @param centerY Center point of movement circle, y. Universal units.
	 * @param radius Radius of movement circle. Universal units.
	 * @param speed Universal units. */
	public CircleMove(double centerX, double centerY, double radius, double speed)
	{
		this.centerX = Coords.x(centerX);
		this.centerY = Coords.y(centerY);
		this.radius = Coords.mulByDiag(radius);
		if (speed < 0.05)
		{
			this.speed = Coords.mulByDiag(speed);
		}
		else
		{
			this.speed = speed;
		}
		direction = Const.RIGHT;
	}

	/** @param centerX Center point of movement circle, x. Universal units.
	 * @param centerY Center point of movement circle, y. Universal units.
	 * @param radius Radius of movement circle. Universal units.
	 * @param speed Universal units.
	 * @param startDegree Starting degree, from 0 to 360. Values out of that range will be taken modulo. */
	public CircleMove(double centerX, double centerY, double radius, double speed, int startDegree)
	{
		this.centerX = Coords.x(centerX);
		this.centerY = Coords.y(centerY);
		this.radius = Coords.mulByDiag(radius);
		this.speed = speed;
		degree = startDegree;
		direction = Const.RIGHT;
	}

	/** @param centerX Center point of movement circle, x. Universal units.
	 * @param centerY Center point of movement circle, y. Universal units.
	 * @param radius Radius of movement circle. Universal units.
	 * @param speed in degrees per millisecond.
	 * @param startDegree Starting degree, from 0 to 360. Values out of that range will be taken modulo.
	 * @param direction Left or right. */
	public CircleMove(double centerX, double centerY, double radius, double speed, int startDegree, int direction)
	{
		this.centerX = Coords.x(centerX);
		this.centerY = Coords.y(centerY);
		this.radius = Coords.mulByDiag(radius);
		this.speed = speed;
		degree = startDegree;
		this.direction = direction;
	}

	@Override
	public void calculateNextPosition(Element element, int interval)
	{
		if (direction == Const.RIGHT)
		{
			degree += speed * interval;
			degree %= 360;
		}
		else
		{
			degree -= speed * interval;
			degree %= -360;
		}
		nextX = (int) (centerX + (radius * Math.cos((Math.PI * degree) / 180)));
		nextY = (int) (centerY + (radius * Math.sin((Math.PI * degree) / 180)));
	}

	@Override
	public void speedUp(double scale)
	{
		speed *= scale;
	}
}
